/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.minimalismstyle.fresh.common.data.commons.service

import com.gitee.minimalismstyle.fresh.common.core.domain.QueryBody
import com.gitee.minimalismstyle.fresh.common.core.domain.QueryParams
import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.domain.Simple
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.web.bind.annotation.*

/**
 * 基础业务接口，提供通用的查询方法
 * @author maoxiaodong
 */
interface BaseService<T, ID> {

    /**
     * 根据ID查询t数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询")
    @GetMapping("findById/{id}")
    fun findById(
            @ApiParam(value = "主键ID", required = true)
            @PathVariable("id")
            id: ID
    ): R<T>

    /**
     * 根据多个ID查询t列表数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据多个ID查询")
    @PostMapping("findByIds")
    fun findByIds(@ApiParam(value = "主键ID集合", required = true) @RequestBody ids: List<ID>): R<List<T>>

    /**
     * 查询全部t列表数据
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    fun findAll(): R<List<T>>

    /**
     * 保存t数据，新增或者更新（id为null）
     * @param t
     * @return
     */
    @ApiOperation(value = "新增或者更新")
    @PutMapping("save")
    fun save(@RequestBody t: T): R<T>

    /**
     * 保存t数据，新增或者更新（id为null）
     * @param ts
     * @return
     */
    @ApiOperation(value = "新增或者更新多个")
    @PutMapping("saveAll")
    fun saveAll(@RequestBody ts: List<T>): R<List<T>>

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{id}")
    fun deleteById(@ApiParam(value = "主键ID", required = true)
                   @PathVariable("id")
                   id: ID
    ): R<Int>


    /**
     * 根据多个ID删除数据
     * @param ids
     * @return
     */
    @DeleteMapping("deleteByIds")
    fun deleteByIds(@ApiParam(value = "主键ID集合", required = true)@RequestBody ids: List<ID>): R<Int>

    /**
     * 验证唯一字段是否存在
     * @param t
     * @return
     */
    @PostMapping("isUnique")
    fun isUnique(t: T): R<Boolean>

    @RequestMapping("findByQueryParams")
    fun findByQueryParams(params: QueryParams<Simple>): R<QueryBody<Simple>>
}